let choice = document.querySelector("textarea");
let tags = document.querySelector(".tags");
let choiceArray = [];
console.log(tags);
choice.addEventListener("keyup", function (event) {
  if (event.keyCode == "13") {
    choice.value = choice.value.replace(/\n/g, ""); // 替换所有回车符为 1
    let length = choiceArray.length;
    if (length == 0) return;
    let count = 0; // 计数器
    const maxCount = 17; // 最大执行次数
    let tagList = document.querySelectorAll(".tag");
    let now = -1;
    let before = -1;
    let intervalId = setInterval(() => {
      if (before != -1) {
        tagList[before].style.backgroundColor = "#f0932b";
      }
      count++;
      now = Math.floor(Math.random() * (length - 1 + 1));
      before = now;
      tagList[now].style.backgroundColor = "#273c75";
      if (count >= maxCount) {
        clearInterval(intervalId); // 停止 interval
        console.log("interval 已停止");
      }
    }, 100); // 每 1000 毫秒（1 秒）执行一次
  } else {
    choiceArray = choice.value.split(/\s+/);
    tags.innerHTML = "";
    let length = choiceArray.length;
    if (choiceArray.length > 0 && choiceArray[length - 1] == "") {
      choiceArray.pop();
    }
    choiceArray.forEach((choice) => {
      let newSpan = document.createElement("div");
      newSpan.textContent = choice;
      newSpan.classList.add("tag");
      tags.appendChild(newSpan);
    });
  }
});
